package com.tansun.easycare.rule.bakproject.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tansun.easycare.core.config.Global;
import com.tansun.easycare.rule.bakproject.service.IRuleProjectZipService;
import com.tansun.easycare.rule.bakproject.service.IRuleProjetBakService;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.rule.utils.LoggerManager;
import com.tansun.rule.utils.ZipUtils;

@Service
public class RuleProjectZipServiceImpl implements IRuleProjectZipService {

	@Autowired
	private  IRuleProjetBakService  ruleProjectBakService;

	public InputStream bakRuleProject(String projectCode) throws BizException {
		ruleProjectBakService.bakRuleProject(projectCode);
		String  ruleBakPath  = Global.getConfig("RuleProject.BakPath")+File.separator+projectCode;
		File  file  = new  File(ruleBakPath);
		FileInputStream fis = null;
		if (file.exists()) {
            ZipUtils.antzip(ruleBakPath, ruleBakPath+".zip", true);
            File  zipFile  = new  File(ruleBakPath+".zip");
            try {
			    fis  = new  FileInputStream(zipFile);
			} catch (Exception e) {
				throw new  BizException(e, "");
			}
            
		}
		return fis;
	}

	@Override
	public void restoreRuleProject(InputStream  is ,String projectCode,String  dataOprType) throws BizException {
		String  ruleBakPath  = Global.getConfig("RuleProject.BakPath")+File.separator+projectCode;
        File  file  = new  File(ruleBakPath);
        if (file.exists()) {
        	file.delete();
        }
        file.mkdirs();
        
        FileOutputStream   fos = null;
        
        try {
        	File  zipfile  = new File(ruleBakPath+".zip");
        	if(zipfile.exists()) {
        		zipfile.delete();
        	}
        	zipfile.createNewFile();
        	fos   = new   FileOutputStream(zipfile);
        	byte[]  b = new  byte[1024];
        	while ( (is.read(b) !=-1)) {
        		fos.write(b);
        	}

        }catch(Exception e) {
        	throw  new  BizException(e, "");
        }finally {
        	if (is != null) {
        		try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        	if (fos != null) {
        		try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        }
    	ZipUtils.unZip(ruleBakPath+".zip");
    	ruleProjectBakService.restoreRuleProject(projectCode,dataOprType);
		

	}

	@Override
	public String parseProjectCode(InputStream inputStream) throws BizException {
		ZipInputStream zipInputStream = new ZipInputStream(inputStream); //以io流的方式获取文件数据，并进行解压操作
			try {
				ZipEntry nextEntry = zipInputStream.getNextEntry();
				return nextEntry.getName();  //返回压缩文件的内部父文件夹名称

			} catch (Exception e) {
				LoggerManager.error("获取解压文件名错误！",e);
				return e.getMessage();
			}
			
		
		
	}
		

}
